﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace JunQuanPrinter
{
    public struct TPmParamData
    {
        public Int32 WireLen;
        public String Font;
        public Byte FontSize;
        public UInt16 Interval;
        public UInt16 Speed;
        public float FontWide;
        public Byte HeadOrEndBlankNum;
        public Byte PrintMode;
        public UInt16 StartLen; //初始位置，一条新的喷码信息的开始点到右侧剥头的距离
    }
    class CCommad
    {
        private static CCommad m_Obj = null;
        private CCommSerial m_commSer = null;
        private TPmParamData m_PmParamData = new TPmParamData();
        public static CCommad getClass()
        {
            if(m_Obj == null)
            {
                m_Obj = new CCommad();
            }
            return m_Obj;
        }

        public void SetParam(CCommSerial commSer)
        {
            m_commSer = commSer;
        }

        public CCommSerial getCommSerial()
        {
            return m_commSer;
        }

        #region 中联重科三段喷码
        /// <summary>
        /// 三段喷码,中联重科
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="blnFontFx"></param>
        /// <returns></returns>
        public Boolean SendCMD_9B(String Text, String Text1, String Text2,UInt16 tachoDiv, String Font, Byte FontSize, UInt16 Interval, UInt16 endSpace, UInt16 wireLen, UInt16 StartLen, UInt16 Speed, Boolean blnFontFx = false)
        {
            int num5;
            string Word_A = Text;
            string Word_B = Text1;
            string Word_C = Text2;
            string Text_A = "";
            string Text_B = "";
            string Text_C = "";
            double lenX1;  //从喷码开始到右端剥头的长度，也等于startLen
            double lenX2;  //从左端开始到喷码结束的长度
            double n, m;  //分别是两端喷码内容中的重复次数
            //UInt16 endSpace;
            Boolean endToEndPrint = false;
            double temp = 0;
            String tempStr = "";
            String tempStr2 = "";
            String fullStr = "";
            int counter_1, counter_2;

            if (!string.Equals(Text, "") && !string.Equals(Text2, ""))
            {
                endToEndPrint = true;
            }

            //lenX1 = (text1.length + interval) x n + text2.length + 首尾留白长度
            //假设lenX1 = 60， 那么60 - text2.length - 留白长度 = n x (text1.length + interval)

            //lenX2 = wireLen - lenX1
            //lenX2 = text.length + 留白长度 + m x (text1.length + interval)
            //假设wireLen = 120, 那么 60 -text.length - 留白长度 = m x (text1.length + interval)

            //UInt16 Speed = m_PmParamData.Speed;

            //wirelen = lenX1 + lenX2 = 喷码长度 = 线长
            //UInt16 wireLen = (UInt16)m_PmParamData.WireLen;
            //String Font = m_PmParamData.Font;  //"5点阵"; //
            //Byte FontSize = m_PmParamData.FontSize;
            //UInt16 Interval = m_PmParamData.Interval;
            double fontwidth = 1.5;
            double wirelen, startlen, endspace, intervalLen;
            //intervalLen = 7;
            wirelen = Math.Round(wireLen / fontwidth);
            startlen = Math.Round(StartLen/ fontwidth);
            endspace = Math.Round(endSpace / fontwidth);
            lenX1 = startlen;
            if (Interval <= 2)
            {
                intervalLen = Math.Round(7/ fontwidth);
            }
            else
            {
                intervalLen = Math.Round(7 + (Interval - 2.0) * 2.5)/ fontwidth;
            }
            wirelen = wirelen - intervalLen;
            //endSpace = m_PmParamData.HeadOrEndBlankNum;
            n = (lenX1 - Text2.Length - endspace) / (Text1.Length + intervalLen);
            n = Math.Round(n);
            lenX2 = (ushort)(wirelen - lenX1);
            m = (lenX2 - Text.Length - endspace) / (Text1.Length + intervalLen);
            m = Math.Round(m);
            if (n<0)
            {
                m = m + n;
            }
            //计算多出的长度
            //double lenRemain = (m + n) * (Text1.Length + intervalLen) + Text.Length + Text2.Length + 2 * endspace - wirelen;
            counter_1 = (int)(lenX1 - Text2.Length - endspace);
            counter_2 = (int)(lenX2 - Text.Length - endspace);
            //if(Interval<2)
            //{
            //    Interval = 2;
            //}

            //if(wireLen <= 200)
            //{
            //    Interval = 25;
            //}

            int num4 = 0;
            if ("5点阵".Equals(Font))
            {
                num4 = 0xfd;  //253  5点阵 反转字体
            }
            if ("7点阵".Equals(Font))
            {
                num4 = 0xfb; //251   7点阵 反转字体
            }

            int num = 0x11a;
            int num2 = 0x11a;
            if ("5点阵".Equals(Font))
            {
                num = 0x11a;
                num2 = 0x11a;
            }
            if ("7点阵".Equals(Font))
            {
                num = 0x11b;
                num2 = 0x11b;
            }
            if ("11点阵".Equals(Font))
            {
                num = 0x11d;
                num2 = 0x11d;
            }
            byte[] buffer = new byte[0x2800];
            int index = 0;
            buffer[index++] = 0x9b;
            buffer[index++] = 3;
            buffer[index++] = 0x10;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 3;
            buffer[index++] = 0x10;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0x11;
            buffer[index++] = 7;
            buffer[index++] = 0x54;
            buffer[index++] = 0x69;
            buffer[index++] = 0x67;
            buffer[index++] = 0x65;
            buffer[index++] = 0x72;
            buffer[index++] = 0x31;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 1;
            buffer[index++] = 0x6a;
            buffer[index++] = 0x69;
            buffer[index++] = 0x61;
            buffer[index++] = 0x20;
            buffer[index++] = 100;
            buffer[index++] = 0x75;
            buffer[index++] = 0x6f;
            buffer[index++] = 0x20;
            buffer[index++] = 0x62;
            buffer[index++] = 0x61;
            buffer[index++] = 0x6f;
            buffer[index++] = 0x20;
            buffer[index++] = 0x63;
            buffer[index++] = 0x61;
            buffer[index++] = 110;
            buffer[index++] = 0x20;
            buffer[index++] = 0x6d;
            buffer[index++] = 0x73;
            buffer[index++] = 0x67;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 2;
            ///for print parameters
            buffer[index++] = 1;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0x12;
            if (!blnFontFx) //字体反相
            {
                buffer[index++] = 0x14;  //自动模式 原来:0x10
            }
            else
            {
                buffer[index++] = 0x94;  //自动模式 原来:0x10
            }
            buffer[index++] = 0;
            buffer[index++] = 2;    //Dtop flter * 100µs [0x02 .. 0x7F]
            buffer[index++] = (byte)tachoDiv;    //Tacho division 分频比
            buffer[index++] = 0;
            buffer[index++] = 3;
            buffer[index++] = 0;
            buffer[index++] = 3;
            buffer[index++] = (byte)(Interval / 0x100);//0;  //喷码间隔
            buffer[index++] = (byte)(Interval % 0x100); //2;
            buffer[index++] = (byte)(Speed / 0x100); // 0x13;     //Conveyor speed in mm/s 4960
            buffer[index++] = (byte)(Speed % 0x100); //0x60;
            buffer[index++] = 0;        //Algo number
            if ("5点阵".Equals(Font))
            {
                buffer[index++] = 0x27;
            }
            if ("7点阵".Equals(Font))
            {
                buffer[index++] = 0; // 0;
            }
            //Number of printed lines
            buffer[index++] = 9;
            buffer[index++] = 1;  //原来是3
            buffer[index++] = 0;
            buffer[index++] = 4;

            //Delimiter for frst line
            buffer[index++] = 10;
            #region first block
            //Defnition of frst block:
            buffer[index++] = 0x10;                      //Type
            buffer[index++] = 0;
            buffer[index++] = 0x12;                      //Length
            buffer[index++] = (byte)(num / 0x100);       //Font
            buffer[index++] = (byte)(num % 0x100);
            buffer[index++] = 0;                         //固定
            buffer[index++] = 0;                         //固定
            buffer[index++] = 0;
            buffer[index++] = 1;                         //Y reference (in number of dot 1 to 32)  Y坐标
            buffer[index++] = 0;                         //固定
            buffer[index++] = FontSize;                  //加粗字体
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0x12;
            buffer[index++] = 0x10;

            buffer[index++] = 30;   //1E
            buffer[index++] = 1;    //01
            buffer[index++] = 30;   //1E
            if (endToEndPrint == false)  //如果只有中间段有文字
            {
                for (num5 = 0; num5 < Word_B.Length; num5++)
                {
                    buffer[index++] = (byte)Word_B[num5];
                }
            }
            else //三段都有文字
            {
                //如果长度超出
                //if (lenRemain > 0)
                //{
                //    n = n - 1;
                //    temp = Text1.Length + intervalLen - lenRemain;
                //    for (int i = 0; i < temp; i++)
                //    {
                //        tempStr2 = tempStr2 + "*";
                //    }
                //}
                for (int i = 0; i < intervalLen; i++)
                {
                    Word_B = Word_B + " ";   //把间隙加上去，这个间隙和每一次喷码之间的间隙是一样的
                }
                tempStr = Word_B;  //假设word_A 是abc，interval是5的话
                for (int i = 0; i < n; i++)
                {
                    Text_A = Text_A + tempStr;  //n如果是2，那么text_A就是“abc     abc     ”
                }
                //Text_A = tempStr2 + Text_A;
                for (num5 = 0; num5 < Text_A.Length; num5++)
                {
                    if (counter_1 <= 0)
                    {
                        break;
                    }
                    buffer[index++] = (byte)Text_A[num5];
                    fullStr = fullStr + Text_A[num5];
                    counter_1--;
                }
                //把多余的字符去掉
                //while (counter_1 < 0)
                //{
                //    buffer[index--] = 0;
                //    counter_1++;
                //}
                //(lenX1 - Text.Length - endSpace) 是这部分字符串的总长，去掉Text_A所占用的长度后，剩下的应该都是空格" "
                temp = (lenX1 - Text.Length - endspace) - Text_A.Length;
                //if (lenRemain <= 0)  //如果长度未超出
                //{
                    for (int i = 0; i < temp; i++)
                    {
                        buffer[index++] = (byte)' ';
                        fullStr = fullStr + "*";
                    }
                //}
                    
            }


            buffer[index++] = 0x10;
            buffer[index++] = 0;
            buffer[index++] = 0x12;
            buffer[index++] = (byte)(num / 0x100);
            buffer[index++] = (byte)(num % 0x100);
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 1;
            buffer[index++] = 0;
            buffer[index++] = FontSize;             //加粗字体
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0x12;
            buffer[index++] = 0x10;

            #endregion
            //三段喷
            if (endToEndPrint == true)
            {
                int num3 = 253;
                #region second block
                //definition of second block:
                buffer[index++] = 0x10;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = (byte)(num / 0x100);
                buffer[index++] = (byte)(num % 0x100);
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 1;
                buffer[index++] = 0;
                buffer[index++] = 1;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = 0x10;

                //definition of text
                //tempStr = strReverse(Word_B);
                //Word_B = tempStr;

                buffer[index++] = 30;   //1E
                buffer[index++] = 1;    //01
                buffer[index++] = 30;   //1E

                Text_B = Word_C;
                for (int i = 0; i < (endspace * 2); i++)
                {
                    Text_B = Text_B + " ";
                }
                //Text_B = Text_B + Word_A;
                for (num5 = 0; num5 < Text_B.Length; num5++)
                {
                    buffer[index++] = (byte)Text_B[num5];
                fullStr = fullStr + Text_B[num5];
                }


                buffer[index++] = 0x10;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = (byte)(num / 0x100);
                buffer[index++] = (byte)(num % 0x100);
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 1;
                buffer[index++] = 0;
                buffer[index++] = 1;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = 0x10;
                //end of second block ^^
                #endregion

                #region third block

                //definition of third block:
                buffer[index++] = 0x10;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = (byte)(num4 / 0x100);
                buffer[index++] = (byte)(num4 % 0x100);
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 1;
                buffer[index++] = 0;
                buffer[index++] = 1;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = 0x10;

                //definition of text

                buffer[index++] = 30;   //1E
                buffer[index++] = 1;    //01
                buffer[index++] = 30;   //1E
                Word_A = strReverse(Word_A);
                Text_B = Word_A;
                for (num5 = 0; num5 < Text_B.Length; num5++)
                {
                    buffer[index++] = (byte)Text_B[num5];
                    fullStr = fullStr + Text_B[num5];
                }
                buffer[index++] = 0x10;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = (byte)(num4 / 0x100);
                buffer[index++] = (byte)(num4 % 0x100);
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 1;
                buffer[index++] = 0;
                buffer[index++] = 1;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = 0x10;
                //end of third block ^^
                #endregion

                #region forth block
                //Defnition of forth block:
                buffer[index++] = 0x10;                      //Type
                buffer[index++] = 0;
                buffer[index++] = 0x12;                      //Length
                buffer[index++] = (byte)(num / 0x100);       //Font
                buffer[index++] = (byte)(num % 0x100);
                buffer[index++] = 0;                         //固定
                buffer[index++] = 0;                         //固定
                buffer[index++] = 0;
                buffer[index++] = 1;                         //Y reference (in number of dot 1 to 32)  Y坐标
                buffer[index++] = 0;                         //固定
                buffer[index++] = FontSize;                  //加粗字体
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = 0x10;

                buffer[index++] = 30;   //1E
                buffer[index++] = 1;    //01
                buffer[index++] = 30;   //1E

                Word_B = Text1;

                for (int i = 0; i < intervalLen; i++)
                {
                    Word_B = " " + Word_B;   //把间隙加上去，这个间隙和每一次喷码之间的间隙是一样的
                }
                tempStr = Word_B;  //假设word_B 是abc，interval是5的话
                for (int i = 0; i < m; i++)
                {
                    Text_C = Text_C + tempStr;  //m如果是2，那么text_C就是“     abc     abc”
                }
                for (num5 = 0; num5 < Text_C.Length; num5++)
                {
                    if (counter_2 <= 0)
                    {
                        break;
                    }
                    buffer[index++] = (byte)Text_C[num5];
                    fullStr = fullStr + Text_C[num5];
                    counter_2--;
                }
                //(lenX1 - Text.Length - endSpace) 是这部分字符串的总长，去掉Text_A所占用的长度后，剩下的应该都是空格" "
                //while (counter_2 < 0)
                //{
                //    buffer[index--] = 0;
                //    counter_2++;
                //}
                temp = (lenX2 - Text.Length - endspace) - Text_C.Length;
                //if (lenRemain <= 0)  //如果长度未超出
                //{ 
                    for (int i = 0; i < temp; i++)
                    {
                        buffer[index++] = (byte)' ';
                        fullStr = fullStr + "*";
                    }
                //}

                buffer[index++] = 0x10;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = (byte)(num / 0x100);
                buffer[index++] = (byte)(num % 0x100);
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 1;
                buffer[index++] = 0;
                buffer[index++] = FontSize;             //加粗字体
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0;
                buffer[index++] = 0x12;
                buffer[index++] = 0x10;

                #endregion
            }


            buffer[index++] = 13;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index++] = 0;
            buffer[index] = 0;
            buffer[1] = (byte)((index - 3) / 0x100);
            buffer[2] = (byte)((index - 3) % 0x100);
            buffer[5] = (byte)((index - 3) / 0x100);
            buffer[6] = (byte)((index - 3) % 0x100);
            for (num5 = 0; num5 < index; num5++)
            {
                buffer[index] = (byte)(buffer[num5] ^ buffer[index]);
            }

            Byte[] revBuf = new Byte[1];

            int ret = m_commSer.SendCommand(buffer, index + 1, 5000, ref revBuf);
            if (ret <= 0)
                return false;
            if (revBuf[0] == 0x06)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 普通版喷码
        //public Boolean SendCMD_9B(String Text, String Text1, String Text2, String Font, Byte FontSize, UInt16 Interval, UInt16 Speed, Boolean blnFontFx = false)
        //{
        //    int num5;
        //    string Word_A = Text;
        //    string Word_B = Text1;
        //    string Word_C = Text2;
        //    int conPos = 45;

        //    int num = 0x11a;
        //    int num2 = 0x11a;
        //    if ("5点阵".Equals(Font))
        //    {
        //        num = 0x11a;
        //        num2 = 0x11a;
        //    }
        //    if ("7点阵".Equals(Font))
        //    {
        //        num = 0x11b;
        //        num2 =0x11b;
        //    }
        //    byte[] buffer = new byte[0x2800];
        //    int index = 0;
        //    buffer[index++] = 0x9b;  //发送一个任务
        //    buffer[index++] = 3;  //长度1   0x310 - 784 
        //    buffer[index++] = 0x10;  //长度2

        //    #region header
        //    #region Total length of data 4bytes
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 3;  //3
        //    buffer[index++] = 0x10;  //4
        //    #endregion
        //    #region checksum 4bytes
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    #endregion


        //    #region job type and version 2bytes
        //    buffer[index++] = 0x11;  //9
        //    buffer[index++] = 7;
        //    #endregion


        //    #region job name 20bytes
        //    buffer[index++] = 0x54;
        //    buffer[index++] = 0x69;
        //    buffer[index++] = 0x67;
        //    buffer[index++] = 0x65;
        //    buffer[index++] = 0x72;
        //    buffer[index++] = 0x31;  //15
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    #endregion

        //    #region job number 2bytes
        //    buffer[index++] = 0;
        //    buffer[index++] = 1;  //31
        //    #endregion

        //    #region Comment 32bytes
        //    buffer[index++] = 0x6a;
        //    buffer[index++] = 0x69;
        //    buffer[index++] = 0x61;
        //    buffer[index++] = 0x20;
        //    buffer[index++] = 100;
        //    buffer[index++] = 0x75;
        //    buffer[index++] = 0x6f;
        //    buffer[index++] = 0x20;
        //    buffer[index++] = 0x62;
        //    buffer[index++] = 0x61;
        //    buffer[index++] = 0x6f;
        //    buffer[index++] = 0x20;
        //    buffer[index++] = 0x63;
        //    buffer[index++] = 0x61;
        //    buffer[index++] = 110;
        //    buffer[index++] = 0x20;
        //    buffer[index++] = 0x6d;
        //    buffer[index++] = 0x73;
        //    buffer[index++] = 0x67;  //50
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    #endregion

        //    #region number of parameters 2 bytes
        //    buffer[index++] = 0;
        //    buffer[index++] = 2;  //65
        //    #endregion


        //    #endregion



        //    #region Job parameter
        //    ///for print parameters
        //    #region Parameter type 1 byte

        //    buffer[index++] = 1;
        //    #endregion

        //    #region parameter number 1 byte
        //    buffer[index++] = 0;
        //    #endregion

        //    #region length 2 byte
        //    buffer[index++] = 0;
        //    buffer[index++] = 0x12;  //69   //原始为12
        //    #endregion

        //    #region global parameters 1 byte
        //    if (!blnFontFx) //字体反相
        //    {
        //        buffer[index++] = 0x14;  //自动模式 原来:0x10
        //    }
        //    else
        //    {
        //        buffer[index++] = 0x94;  //自动模式 原来:0x10
        //    }
        //    #endregion



        //    #endregion


        //    buffer[index++] = 0;
        //    buffer[index++] = 2;    //Dtop flter * 100µs [0x02 .. 0x7F]
        //    buffer[index++] = 5;    //Tacho division 分频比
        //    buffer[index++] = 0;
        //    buffer[index++] = 3;
        //    buffer[index++] = 0;
        //    buffer[index++] = 3;
        //    buffer[index++] = (byte)(Interval / 0x100);//0;  //喷码间隔
        //    buffer[index++] = (byte)(Interval % 0x100); //2;  
        //    buffer[index++] = (byte)(Speed / 0x100); // 0x13;     //喷码速度Conveyor speed in mm/s 4960
        //    buffer[index++] = (byte)(Speed % 0x100); //0x60;
        //    buffer[index++] = 0;        //Algo number
        //    if ("5点阵".Equals(Font))
        //    {
        //        buffer[index++] = 0x27;
        //    }
        //    if ("7点阵".Equals(Font))
        //    {
        //        buffer[index++] = 0; // 0;
        //    }
        //    //Number of printed lines
        //    buffer[index++] = 9;
        //    buffer[index++] = 1;  //原来是3
        //    buffer[index++] = 0;
        //    buffer[index++] = 4;

        //    //Delimiter for frst line
        //    buffer[index++] = 10;
        //    //Defnition of frst block:
        //    buffer[index++] = 0x10;     //parameter type                  //Type
        //    buffer[index++] = 0;
        //    buffer[index++] = 0x12;                      //Length
        //    buffer[index++] = (byte)(num / 0x100);       //Font
        //    buffer[index++] = (byte)(num % 0x100);      //Font
        //    buffer[index++] = 0;                         //固定  algorithm number
        //    buffer[index++] = 0;                         //固定  algorithm number
        //    buffer[index++] = 0;                        //Y reference
        //    buffer[index++] = 1;                         //Y reference (in number of dot 1 to 32)  Y坐标
        //    buffer[index++] = 0;                         //固定 
        //    buffer[index++] = FontSize;                  //加粗字体 expansion
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0x12;
        //    buffer[index++] = 0x10;
        //    //definition of text:
        //    buffer[index++] = 30;   //1E
        //    buffer[index++] = 1;    //01
        //    buffer[index++] = 30;   //1E

        //    for (num5 = 0; num5 < Word_A.Length; num5++)
        //    {
        //        buffer[index++] = (byte)Word_A[num5];
        //    }

        //    buffer[index++] = 0x10;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0x12;
        //    buffer[index++] = (byte)(num / 0x100);
        //    buffer[index++] = (byte)(num % 0x100);
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 1;
        //    buffer[index++] = 0;
        //    buffer[index++] = FontSize;             //加粗字体
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0x12;
        //    buffer[index++] = 0x10;

        //    int num3 = 253;
        //    #region second block
        //    //definition of second block:
        //    buffer[index++] = 0x10;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0x12;
        //    buffer[index++] = (byte)(num3 / 0x100);
        //    buffer[index++] = (byte)(num3 % 0x100);
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 1;
        //    buffer[index++] = 0;
        //    buffer[index++] = 1;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0x12;
        //    buffer[index++] = 0x10;

        //    //definition of text
        //    string tempStr = strReverse(Word_B);
        //    Word_B = tempStr;

        //    buffer[index++] = 30;   //1E
        //    buffer[index++] = 1;    //01
        //    buffer[index++] = 30;   //1E

        //    for (num5 = 0; num5 < Word_B.Length; num5++)
        //    {
        //        buffer[index++] = (byte)Word_B[num5];
        //    }
            

        //    if ("5点阵".Equals(Font))
        //    {
        //        num2 = 0xfd;  //253  5点阵 反转字体
        //    }
        //    if ("7点阵".Equals(Font))
        //    {
        //        num2 = 0xfb; //251   7点阵 反转字体
        //    }
        //    buffer[index++] = 0x10;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0x12;
        //    buffer[index++] = (byte)(num2 / 0x100);
        //    buffer[index++] = (byte)(num2 % 0x100);
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 1;
        //    buffer[index++] = 0;
        //    buffer[index++] = 1;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0x12;
        //    buffer[index++] = 0x10;
        //    //end of second block ^^
        //    #endregion


        //    buffer[index++] = 13;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index++] = 0;
        //    buffer[index] = 0;
        //    buffer[1] = (byte)((index - 3) / 0x100);
        //    buffer[2] = (byte)((index - 3) % 0x100);
        //    buffer[5] = (byte)((index - 3) / 0x100);
        //    buffer[6] = (byte)((index - 3) % 0x100);
        //    for (num5 = 0; num5 < index; num5++)
        //    {
        //        buffer[index] = (byte)(buffer[num5] ^ buffer[index]);
        //    }

        //    Byte[] revBuf = new Byte[1];

        //    Int32 ret = m_commSer.SendCommand(buffer, index + 1, 5000, ref revBuf);
        //    if (ret != 1)
        //        return false;
        //    if(revBuf[0] == 0x06)
        //    {
        //        return true;
        //    }
        //    return false;
        //}

        #endregion


        private static string strReverse(string string_1)
        {
            char[] array = string_1.ToCharArray();
            Array.Reverse(array);
            return new string(array);
        }

        private bool isSendSuccess(SerialPort port)
        {
            long tickCount = Environment.TickCount;
            do
            {
                byte[] buffer = new byte[0x400];
                try
                {
                    if (port.BytesToRead > 0)
                    {
                        port.Read(buffer, 0, buffer.Length);
                    }
                }
                catch
                {
                }
                if (buffer[0] != 0)
                {
                    return (buffer[0] == 6);
                }
            }
            while ((Environment.TickCount - tickCount) < 0x1388L);
            return false;
        }

        private bool ReadC4()
        {
            int tickCount = Environment.TickCount;
            try
            {
                SerialPort serPort = m_commSer.getSerial();
                long num2 = Environment.TickCount;
                do
                {
                    Thread.Sleep(10);
                    int bytesToRead = serPort.BytesToRead;
                    byte[] buffer = new byte[bytesToRead];
                    serPort.Read(buffer, 0, bytesToRead);
                    byte[] buffer2 = new byte[] { 0xc5, 0, 1, 0, 0xc4 };
                    if (CTool.arrayEquals(buffer, buffer2))
                    {
                        return true;
                    }

                    Application.DoEvents();
                }
                while ((Environment.TickCount - num2) < 0x4e20L);
            }
            catch (Exception exception)
            {
                CTool.ShowMsg(exception.Message);
            }
            return false;
        }

        public Boolean SendCMD_98()
        {
            if (this.ReadC4())
            {
                SerialPort serPort = m_commSer.getSerial();
                byte[] buffer2 = new byte[] { 6 };
                serPort.Write(buffer2, 0, buffer2.Length);
                buffer2 = new byte[] { 0x98, 0, 2, 0, 1, 0x9b };
                serPort.Write(buffer2, 0, buffer2.Length);
                if (this.isSendSuccess(serPort))
                {
                    return true;
                }

            }
            return false;

        }

        //public Boolean SendData(String Text1, String Text2, String Text3, Boolean blnFontFx = false)//String Text2, 
        //{
        //    if (0 == m_PmParamData.PrintMode)
        //    {
        //        if (!SendCMD_9B(Text1, Text2, Text3, blnFontFx))
        //        {
        //            return false;
        //        }
        //    }
        //    else
        //    {
        //        if (!SendCMD_9B(Text1, Text2, Text3, blnFontFx)) //if (!SendCMD_9B(Text1, Text2, blnFontFx))
        //        {
        //            return false;
        //        }
        //    }

        //    if (!SendCMD_98())
        //    {
        //        return false;
        //    }

        //    return true;
        //}

    }
}
